

-- Namespace
	local _,mod = ...


-- Object ref
	local RAID_CLASS_COLORS = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS


-- Constants
	local font = {mod.path..'media\\bavaria.ttf',8,'OUTLINEMONOCHROME'}
	
	local c_bar =    {.16,.16,.16}
	local c_barbg =  {.33,.33,.33}
	local c_border = {.07,.07,.07}
	
	local white = { r = 1, g = 1, b = 1 }


-- Helpers
	local function dim(color,perc)
		local r,g,b = color.r,color.g,color.b
		r,g,b = r*perc,g*perc,b*perc
		return r,g,b
	end


for i = 1,4 do
	local unit = ('party%d'):format(i)
	
	-- Base frame
		local partyN = mod:UF(unit)
			partyN:SetFrameLevel(2)
			partyN:SetHeight(44) partyN:SetWidth(14)
			partyN:SetPoint('BOTTOMRIGHT',UIParent,'CENTER',218-((i-1)*48),-214)
			
			partyN:SetScript('OnEnter',function(self)
				GameTooltip_SetDefaultAnchor(GameTooltip,self)
				GameTooltip:SetUnit(self.unit)
			end)
			partyN:SetScript('OnLeave',function(self)
				GameTooltip:ClearLines()
				GameTooltip:Hide()
			end)
		
		local bg = partyN:CreateTexture(nil,'BACKGROUND')
			bg:SetAllPoints(true)
			bg:SetTexture(unpack(c_border))
			partyN.bg = bg


	-- Health
		local hpbar = partyN:CreateTexture(nil,'ARTWORK')
			hpbar:SetHeight(42) hpbar:SetWidth(9)
			hpbar:SetPoint('BOTTOMRIGHT',-1,1)
			hpbar:SetTexture(unpack(c_bar))
			hpbar:Listen('health',function(self,min,max)
				local perc = min / max
				if perc <= 0 then
					perc = .001
				elseif perc > 1 then
					perc = 1
				end
				self:SetHeight(42*perc)
			end)
			partyN.hpbar = hpbar
		
		local inchealbar = partyN:CreateTexture(nil,'ARTWORK')
			inchealbar:SetHeight(42) inchealbar:SetWidth(9)
			inchealbar:SetPoint('BOTTOM',hpbar,'TOP')
			inchealbar:SetTexture(.2,.6,.2)
			inchealbar:Hide()
			inchealbar:Listen('incheal',function(self,heal,min,max)
				if heal then
					self:Show()
					local def = 1 - (min / max)
					local perc = heal / max
					if perc > def then
						perc = def
					end
					if perc <= 0 then
						perc = .001
					elseif perc > 1 then
						perc = 1
					end
					self:SetHeight(42*perc)
				else
					self:Hide()
				end
			end)
			partyN.inchealbar = inchealbar
		
		local hpbg = partyN:CreateTexture(nil,'BORDER')
			hpbg:SetHeight(42) hpbg:SetWidth(9)
			hpbg:SetPoint('BOTTOMRIGHT',-1,1)
			hpbg:SetTexture(unpack(c_barbg))
			partyN.hpbg = hpbg


	-- Power
		local ppbar = partyN:CreateTexture(nil,'ARTWORK')
			ppbar:SetHeight(42) ppbar:SetWidth(2)
			ppbar:SetPoint('BOTTOMLEFT',1,1)
			ppbar:SetTexture(1,1,1)
			ppbar:Listen('power',function(self,min,max)
				local perc = min / max
				if perc <= 0 then
					perc = .001
				elseif perc > 1 then
					perc = 1
				end
				self:SetHeight(42*perc)
			end)
			ppbar:Listen('class',function(self,_,enclass)
				local color = RAID_CLASS_COLORS[enclass] or white
				self:SetTexture(dim(color,.75))
			end)
			partyN.ppbar = ppbar
		
		local ppbg = partyN:CreateTexture(nil,'BORDER')
			ppbg:SetHeight(42) ppbg:SetWidth(2)
			ppbg:SetPoint('BOTTOMLEFT',1,1)
			ppbg:SetTexture(unpack(c_barbg))
			partyN.ppbg = ppbg
	
	
	-- Threat
		local threattick = partyN:CreateTexture(nil,'OVERLAY')
			threattick:SetZLevel(1)
			threattick:SetHeight(2) threattick:SetWidth(9)
			threattick:Hide()
			threattick:SetTexture(.4,.8,.4)
			threattick:Listen('threat',function(self,valid,tank,_,tpct)
				if valid then
					self:Show()
					local tpct = tpct * .01
					if tpct <= 0 then
						tpct = .001
					elseif tpct > 1 then
						tpct = 1
					end
					if tank then
						self:SetTexture(.8,.8,.4)
					else
						self:SetTexture(.4,.8,.4)
					end
					self:ClearAllPoints()
					self:SetPoint('BOTTOMRIGHT',-1,1+40*tpct)
				else
					self:Hide()
				end
			end)
			partyN.threattick = threattick
	
	
	-- Role
		local roletext = partyN:CreateFontString(nil,'OVERLAY')
			roletext:SetPoint('BOTTOMRIGHT',partyN,'BOTTOMLEFT',-2,4)
			roletext:SetFont(unpack(font))
			roletext:SetJustifyH('RIGHT')
			roletext:Listen('role',function(self,role)
				role = (
					role == 'TANK' and 'T' or
					role == 'HEALER' and '+' or
					''
				)
				self:SetText(role)
			end)
			partyN.roletext = roletext
end
